//
//  ZSStatusPictureView.swift
//  ZSWeibo
//
//  Created by zsj1992 on 16/4/14.
//  Copyright © 2016年 ichange. All rights reserved.
//

import UIKit
let imageMargin:CGFloat = 3
let maxW = screenWidth - CGFloat(2 * ZSStatusCellMargin)
let itemW = (maxW - 2 * imageMargin)/3


//pictureView是一个collectionvView


//配图视图的创建-->配图视图就是里面有一个collectionView,配图的数据的加载也需要进行处理
class ZSStatusPictureView: UICollectionView {

    let ID = "colltctionItem"
    

    
    var imageURLs : [ZSPicture]?{
        
        didSet{
            

            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(caculateSize())
                
            
            }
self.reloadData()
        }
    }

    
    //懒加载一个测试的label
    lazy var testLabel:UILabel? = UILabel()

    
    //collectionView的初始化方法
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        //创建一个布局对象
        let layout = UICollectionViewFlowLayout()
     

        
        layout.minimumInteritemSpacing = 3
        layout.minimumLineSpacing = 3

        
        super.init(frame: frame, collectionViewLayout: layout)
        self.backgroundColor = UIColor.clearColor()


        self.addSubview(testLabel!)
        
        testLabel?.textColor = UIColor.redColor()
      
        //设置测试label的约束
        testLabel?.snp_makeConstraints(closure: { (make) -> Void in
            
            make.center.equalTo(self)
            
        })
        
        
        
        
        self.dataSource = self

        //注册collectionViewCell
        self.registerClass(ZSPictureCell.self, forCellWithReuseIdentifier: ID)
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    //计算配图视图的尺寸
    func caculateSize()->CGSize{
        
        
        
        
        //判断图片张数,动态计算collectionView的高宽
        //行数
        let count = imageURLs?.count ?? 0
        
        
//        print(count)
        
        let row : CGFloat = CGFloat((count-1)/3+1)

//        //拿到布局对象
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
       
        layout.itemSize = CGSize(width: itemW, height: itemW)

        
        if count == 0{
        
        return CGSizeZero
        }
        
        
        //1张图片
        if count == 1{
        
        let imageSize = CGSize(width: 100, height: 150)
        
        layout.itemSize = imageSize
            
        return imageSize
          
        }
    
        
        //4张图片
        if count == 4{
            
        let w = itemW * 2 + imageMargin
            
        return CGSize(width: w, height: w)
        }
        
        //走到这里是其他张图片的形式
        
//        let row = CGFloat((imageCount - 1) / 3 + 1)
       
        let h = row * itemW + CGFloat(row - 1) * CGFloat(ZSStatusCellMargin)
        
        return CGSize(width: maxW, height: h)
    }

}


//类扩展中进行数据源方法的实现等

extension ZSStatusPictureView:UICollectionViewDataSource{
    
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      
//        print(imageURLs?.count)
        
        let count = imageURLs?.count ?? 0
        
        testLabel?.text = "\(count)"

        return (imageURLs?.count) ?? 0
    }
    
    
    //这样是可以的
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ID, forIndexPath: indexPath) as! ZSPictureCell
        
        //模型传递
        cell.picture = imageURLs![indexPath.item]
        
        cell.backgroundColor = randomColor()
        
        return cell
    }
    
    
    
}



